package com.logos.musicframe.listener;

import com.google.common.base.Preconditions;
import com.logos.musicframe.constant.MqConstant;
import com.logos.musicframe.entity.PlaylistCollectCount;
import com.logos.musicframe.entity.PlaylistCollected;
import com.logos.musicframe.mapper.PlaylistCollectCountMapper;
import com.logos.musicframe.mapper.PlaylistCollectedMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author logos
 * date 2025/6/19 14:21
 * @version v1.0
 * @Package com.logos.musicframe.listener
 */
@Component
@Slf4j
public class PlaylistListener {

    @Autowired
    private PlaylistCollectCountMapper playlistCollectCountMapper;

    @Autowired
    private PlaylistCollectedMapper playlistCollectedMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstant.PLAYLIST_COLLECTED_QUEUE, durable = "true"),
            exchange = @Exchange(name = MqConstant.PLAYLIST_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = {MqConstant.PLAYLIST_COLLECTED_ROUTING_KEY}
    ))
    public void listenCollectQueue(PlaylistCollected playlistCollected){
        log.info("收到收藏消息：{}", playlistCollected);
        Preconditions.checkNotNull(playlistCollected.getPlaylistId(),"收藏消息的歌单id不能为空");
        Preconditions.checkNotNull(playlistCollected.getUserId(),"收藏消息的用户id不能为空");
        playlistCollected.setCreateTime(new Date());
        playlistCollectedMapper.insert(playlistCollected);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstant.PLAYLIST_UNCOLLECTED_QUEUE, durable = "true"),
            exchange = @Exchange(name = MqConstant.PLAYLIST_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = {MqConstant.PLAYLIST_UNCOLLECTED_ROUTING_KEY}
    ))
    public void listenUnCollectQueue(PlaylistCollected playlistCollected){
        log.info("收到取消收藏消息：{}", playlistCollected);
        Preconditions.checkNotNull(playlistCollected.getPlaylistId(),"取消收藏消息的歌单id不能为空");
        Preconditions.checkNotNull(playlistCollected.getUserId(),"取消收藏消息的用户id不能为空");
        playlistCollectedMapper.deleteByCondition(playlistCollected);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstant.PLAYLIST_COLLECT_COUNT_QUEUE, durable = "true"),
            exchange = @Exchange(name = MqConstant.PLAYLIST_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = {MqConstant.PLAYLIST_COLLECT_COUNT_ROUTING_KEY}
    ))
    public void listenCollectCountQueue(PlaylistCollectCount playlistCollectCount){
        log.info("监听到修改收藏数消息：{}", playlistCollectCount);
        // 先查询是否有这歌单的收藏记录
        PlaylistCollectCount dbCollectCount = playlistCollectCountMapper.queryByPlaylistId(playlistCollectCount.getPlaylistId());
        log.info("数据库中已有该歌单的收藏记录：{}", dbCollectCount);
        if(dbCollectCount == null){
            // 新增
            playlistCollectCountMapper.insert(playlistCollectCount);
        }else{
            // 更新
            long count = dbCollectCount.getCount() + playlistCollectCount.getCount();
            dbCollectCount.setCount(count);
            playlistCollectCountMapper.update(dbCollectCount);
        }
    }
}
